import { result } from 'lodash';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-07 09:06:14
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-25 08:52:55
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BopNodeEditDto, BopNodeServiceProxy, CreateOrUpdateBopNodeInput } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';

@Component({
    selector: 'createOrEditBopTreeModal',
    templateUrl: './create-or-edit-boptree-modal.component.html',
})
export class CreateOrEditBopTreeModalComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Input() parentrow: any;//当前点击节点
    @Input() bopInfo: any;//当前bopInfo
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    saving = false;
    active = false;
    form: BopNodeEditDto = new BopNodeEditDto();//新增树结构实体
    isInsert: true;//是否是新增 true为新增
    saveFlag = true;
    constructor(
        injector: Injector,
        private _bopNodeServiceProxy: BopNodeServiceProxy,
        private _messageService: MessageService,
    ) {
        super(injector);
    }
    //保存树结构
    save(): void {
        //获取实体
        let input = new CreateOrUpdateBopNodeInput();
        //将form表单赋值给input的bopdesc下
        input.bopNode = this.form;
        //父级节点id 新增时需要传父级节点
        if (this.isInsert) {
            input.bopNode.parentId = this.parentrow.id;
        }
        //bopid
        input.bopNode.bopDescId = this.bopInfo.id;
        this.saving = true;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._bopNodeServiceProxy.createOrUpdateBopNode(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                        this.modalSave.emit(true);
                    } else {
                        this._messageService.error(result.message)
                    }

                });
        }

    }
    //打开弹窗
    onShown(): void {
        // document.getElementById('model').focus();
    }
    //查看新增任务
    show(key, row): void {
        this.isInsert = key;
        if (!key) {
            this.form.nodeDrawingNo = row.nodeDrawingNo;
            this.form.nodeName = row.nodeName;
            this.form.parentId = row.parentId;
            this.form.id = row.id;
        }
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form = new BopNodeEditDto();
        this.active = false;
        this.modal.hide();
    }

    // getAssignedRoleCount(): number {
    //     return _.filter(this.roles, { isAssigned: true }).length;
    // }
}
